package com.neusoft.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.neusoft.dao.EmpMapper;
import com.neusoft.entity.Dept;
import com.neusoft.entity.Emp;

@Repository("empMapper")
public class MapperImpl implements EmpMapper {

	private SqlSessionFactory sqlSessionFactory=null;
	private SqlSession sqlSession=null;
	
	public SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	public SqlSession getSqlSession() {
		return sqlSession;
	}

	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

	@Before
	public void init() throws IOException{
		String resource="mybatis-config.xml";
		InputStream inputStream=Resources.getResourceAsStream(resource);
		sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
		sqlSession=sqlSessionFactory.openSession();
	}
	
	@After
	public void destory(){
		sqlSession.close();
	}
	
	@Override
	public List<Emp> getEmpbyDeptno(int deptno) {
		// TODO Auto-generated method stub
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Emp> list= sqlSession.selectList("selectDept", deptno);
		destory();
		return list;
	}

	@Override
	public Emp getEmpbyEmpno(int empno) {
		// TODO Auto-generated method stub
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Emp emp = sqlSession.selectOne("selectEmp", empno);
		destory();
		return emp;
	}

	@Override
	public int insertEmp(Emp emp) {
		// TODO Auto-generated method stub
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sqlSession.insert("insertEmp", emp);
		sqlSession.commit();
		destory();
		return 1;
	}

	@Override
	public int moveEmp(int empno, int deptno) {
		// TODO Auto-generated method stub
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Emp emp = new Emp();
		emp.setEmpno(empno);
		emp.setDeptno(deptno);
		sqlSession.update("updateEmp", emp);
		sqlSession.commit();
		destory();
		return 1;
	}

	@Override
	public int updateTotalSal(int deptno, double totalSal) {
		// TODO Auto-generated method stub
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Dept dept = new Dept();
		dept.setDeptno(deptno);
		dept.setTotalSal(totalSal);
		sqlSession.update("updateTotalSal", dept);
		sqlSession.commit();
		destory();
		return 1;
	}

	@Override
	public int changeEmp(int empno)
	{
		return 0;
	}

}
